﻿using G.Service.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace G.Service.BLL
{
    public static class UserBLL
    {
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <param name="nickname"></param>
        /// <returns></returns>
        public static Result Register(string email, string password, string nickname)
        {
            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
                return new Result() { Msg = "账号或密码不能为空！" };

            string tab = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            if (!System.Text.RegularExpressions.Regex.IsMatch(email, tab))
                return new Result() { Msg = "请输入正确的email" };

            UserData userdata = new UserData();
            if (userdata.GetUserByEmail(email) != null)
                return new Result() { Msg = "该账号已存在！" };

            if (string.IsNullOrEmpty(nickname))
                userdata.AddUser(email, password, email);
            else
                userdata.AddUser(email, password, nickname);

            userdata.SaveChanges();
            return new Result() { IsOK = true };
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static Result<User> Login(string email, string password)
        {
            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
                return new Result<User>() { Msg = "账号或密码不能为空！" };
            UserData userdata = new UserData();
            User user = userdata.Login(email, password);
            if (user == null)
                return new Result<User>() { Msg = "用户名或密码错误" };
            return new Result<User>() { IsOK = true, ResponseData = user };
        }

        public static Result<User[]> GetAllUsers()
        {
               UserData userdata = new UserData();
               return new Result<User[]>() { IsOK = true, ResponseData = userdata.GetAllUsers() };
        }
        
    }
}
